﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using devtm.Collections;

namespace devtm.Editor.TypeSelector.AttributeBuilder
{
    /// <summary>
    /// Interaction logic for TitleChooseAttributeUserControl.xaml
    /// </summary>
    public partial class TitleChoosePropertyTypeUserControl : UserControl, INotifyPropertyChanged
    {


        internal enum ModeEditor
        {
            Text,
            List
        }


        internal ModeEditor Mode = ModeEditor.Text;


        public TitleChoosePropertyTypeUserControl()
        {

            InitializeComponent();

            VisibilityEditor(System.Windows.Visibility.Collapsed);

            Liste.DataContext = this;
            MyLabel.DataContext = this;

            Text.Visibility = System.Windows.Visibility.Collapsed;
            Liste.Visibility = System.Windows.Visibility.Collapsed;

        }

        public void VisibilityEditor(Visibility visible)
        {

            switch (Mode)
            {

                case ModeEditor.Text:
                    Text.Visibility = visible;
                    break;

                case ModeEditor.List:
                    Liste.Visibility = visible;
                    break;

                default:
                    break;
            }


            if (visible == System.Windows.Visibility.Visible)
            {

                MyLabel.Visibility = System.Windows.Visibility.Collapsed;


                RefreshValue();


                Lost(this);
            }
            else
                MyLabel.Visibility = System.Windows.Visibility.Visible;

        }

        private void RefreshValue()
        {
            try
            {

                switch (Mode)
                {
                    case ModeEditor.Text:
                        Parametre.ValueString = Text.Text;
                        Text.Text = Parametre.ValueString;
                        break;

                    case ModeEditor.List:
                        if (Liste.SelectedItem != null)
                        {
                            //Parametre.ValueString = SelectedItem.ToString();
                        }
                        break;

                }

            }
            catch (Exception Ex)
            {
                string e = Ex.Message;
            }
        }

        #region Properties


        public string SelectedItemString
        {
            get { return Parametre.ValueString; }
            set { Parametre.ValueString = value; }
        }

        public Object SelectedItem
        {
            get { return Parametre.Value; }
            set { Parametre.Value = value; }
        }
 

        public new Object DataContext
        {
            get { return base.DataContext; }
            set
            {

                base.DataContext = value;

                var v = value as ITableCollection<Type, string>;

                ViewCollection<Type, string> Subliste = v.GetView(c => Filter(c));

                Liste.ItemsSource = (IEnumerable<Type>)Subliste;

            }
        }

        private bool Filter(Type c)
        {
            return c.GetGenericArguments().Count() == 0;
        }


        #endregion


        #region Events

        private void ComboLostFocus(object sender, RoutedEventArgs e)
        {

            if (_NotAction > DateTime.Now || Liste.IsDropDownOpen)
                return;

            VisibilityEditor(System.Windows.Visibility.Collapsed);

            RefreshValue();

        }


        #endregion


        #region INotifyPropertyChanged

        protected void MyPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, e);
        }

        public void MyPropertyChanged(string propertyName)
        {
            MyPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;
        

        #endregion


        private void LabelMouseDown(object sender, MouseButtonEventArgs e)
        {

            _NotAction = DateTime.Now.AddMilliseconds(800);

            VisibilityEditor(System.Windows.Visibility.Visible);
            Liste.Focus();

        }


        private DateTime _NotAction;
        private void ComboSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            VisibilityEditor(System.Windows.Visibility.Collapsed);
        }


        private bool _isFirst;
        public bool IsFirst 
        {
            get { return _isFirst; }
            set
            {
                _isFirst = value;
                if (value)
                    Img.Content = String.Empty;
                else
                    Img.Content = ",";

            }
        }

        public Action<object> Lost { get; set; }


        private Parameter _parametre = null;
        public Parameter Parametre 
        {
            get
            {
                return _parametre;
            }

            set
            {

                _parametre = value;


                if (_parametre.Type == typeof(bool) || _parametre.Type == typeof(Type) || _parametre.Type.IsEnum)
                    Mode = ModeEditor.List;
                else
                    Mode = ModeEditor.Text;


                if (_parametre.Type.IsEnum)
                {
                    List<string> l = new List<string>();

                    foreach (string item in Enum.GetNames(_parametre.Type))
                        l.Add(item);

                    //base.DataContext = l;
                    Liste.ItemsSource = l;
                }

                else if (_parametre.Type == typeof(bool))
                {
                    List<string> l = new List<string>();

                    l.Add("True");
                    l.Add("False");

                    Liste.ItemsSource = l;

                }



            }
        }

        private void PreviewTextInput(object sender, TextCompositionEventArgs e)
        {

        }

        private void DropDownOpened(object sender, EventArgs e)
        {
            
        }

        

    }
}
